package org.example.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.example.springcloud.result.CommonResult;
import org.example.springcloud.service.PaymentFeignService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 *
 * @author Jandmla
 * Date: 2022/7/22
 * Time: 15:34
 * Description:
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "defaultErrorHandler")
public class OrderHystrixController {

    private final PaymentFeignService service;

    public OrderHystrixController(PaymentFeignService service) {
        this.service = service;
    }

    /**
     * 数据获取
     * @param id ID
     * @return 查询结果
     */
    @GetMapping(value = "consumer/payment/hystrix/ok/{id}")

    public CommonResult<String> getPayment(@PathVariable("id") Long id) {
        return service.paymentInfo_OK(id);
    }

    /**
     * 超时测试
     * @return 端口号
     */
    @GetMapping(value = "consumer/payment/hystrix/timeout/{id}")
//    @HystrixCommand(fallbackMethod = "paymentFeignTimeoutHandler",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
//    })
    @HystrixCommand
    public CommonResult<String> paymentFeignTimeout(@PathVariable("id") Long id){
        int a = 100/0;
        return service.paymentInfo_TimeOut(id);
    }

    public CommonResult<String> paymentFeignTimeoutHandler(@PathVariable("id") Long id){
        return new CommonResult<>(200,"系统繁忙！","我是消费者80，对方支付系统繁忙，请10秒后再试，或者自己运行出错，请检查自己o(╥﹏╥)o！");
    }

    public CommonResult<String> defaultErrorHandler(){
        return new CommonResult<>(500,"处理异常","我是消费者80，对方支付系统繁忙，请稍后再试，或者自己运行出错，请检查自己o(╥﹏╥)o！");
    }

}